package com.bang.low.mapping;

import com.bang.EsUtils;
import org.apache.http.StatusLine;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;

import java.io.IOException;

/**
 * @ClassName: MappingApi
 * @Desc: TODO
 * @author: <Bang>
 * @Date: 2022/01/18 17:29
 * @Version: 1.0
 * @Modified By:
 */
public class MappingApi {

    public static void main(String[] args) throws IOException {
        getIndexMapping("abc/_mapping");
    }

    public static void getIndexMapping(String endpoint) throws IOException {
        final RestClient lowLevelClient = EsUtils.getLowLevelClient();
        final Request request = new Request("GET", endpoint);
        try {
            final Response response = lowLevelClient.performRequest(request);
            final StatusLine statusLine = response.getStatusLine();
            final int statusCode = statusLine.getStatusCode();
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("statusCode:" + statusCode);
            System.out.println("response:" + responseBody);


        } catch (ResponseException e) {
            final Response response = e.getResponse();
            final StatusLine statusLine = response.getStatusLine();
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("statusCode:" + statusLine.getStatusCode());
            System.out.println("response:" + responseBody);
        }

    }
}
